package com.manor.dynamic.adapter;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.lzy.okgo.model.Response;
import com.manor.common.CommonAppConfig;
import com.manor.common.Constants;
import com.manor.common.adapter.RefreshAdapter;
import com.manor.common.bean.UserBean;
import com.manor.common.custom.DrawableTextView;
import com.manor.common.http.CommonHttpUtil;
import com.manor.common.http.HttpCallback;
import com.manor.common.http.JsonBean;
import com.manor.common.http.PitUtil;
import com.manor.common.utils.DialogUitl;
import com.manor.common.utils.DpUtil;
import com.manor.common.utils.KLog;
import com.manor.common.utils.RouteUtil;
import com.manor.common.utils.ScreenDimenUtil;
import com.manor.common.utils.StringUtil;
import com.manor.common.utils.ToastUtil;
import com.manor.common.utils.WordUtil;
import com.manor.dynamic.R;
import com.manor.dynamic.activity.AbsDynamicActivity;
import com.manor.dynamic.activity.AbsDynamicCommentActivity;
import com.manor.dynamic.activity.DynamicDetailsActivity;
import com.manor.dynamic.bean.CommunityDynamicBean;
import com.manor.dynamic.bean.DynamicCommentBean;
import com.manor.dynamic.event.DynamicLikeEvent;
import com.manor.dynamic.http.DynamicHttpUtil;
import com.manor.dynamic.views.AudioMp3View;
import com.manor.im.utils.ImTextRender;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;


/**
 * Created by debug on 2019/7/24.
 * 动态详情
 */

public class DynamicDetailsCommentAdapter extends RefreshAdapter<DynamicCommentBean> {

    private static final int HEAD = -1;
    private static final int TEXT_PARENT = 1;
    private static final int TEXT_CHILD = 2;
    private static final int VOICE_PARENT = 3;
    private static final int VOICE_CHILD = 4;
    private Drawable mLikeDrawable;
    private Drawable mUnLikeDrawable;
    private View.OnClickListener mOnClickListener;
    private View.OnClickListener mLikeClickListener;
    private View.OnClickListener mExpandClickListener;
    private View.OnClickListener mCollapsedClickListener;
    private View.OnClickListener mVoiceClickListener;

    private ActionListener mActionListener;
    private int mCurLikeCommentPosition;
    private DynamicCommentBean mCurLikeCommentBean;
    private HttpCallback mLikeCommentCallback;
    private Drawable mVoicePlay;
    private Drawable mVoicePause;
    private DynamicCommentBean mCurVoiceCommentBean;
    private CommunityDynamicBean mDynamicBean;
    private boolean mIsFromUserCenter;
    private Dialog mChooseVideoDialog;

private String myDelId;
    public void chooseVideo(final String delid) {
        myDelId = delid;
        KLog.e("log","////="+delid);
        if (mChooseVideoDialog == null) {
            mChooseVideoDialog = DialogUitl.getStringArrayDialog(mContext, new Integer[]{
                    R.string.dynamic_del}, true, new DialogUitl.StringArrayDialogCallback() {
                @Override
                public void onItemClick(String text, int tag) {
                    if (tag == R.string.dynamic_del) {
                        KLog.e("log","////="+myDelId);
                        delDynamic(myDelId);
                    }
                }
            });
        }
        mChooseVideoDialog.show();

    }

    public DynamicDetailsCommentAdapter(Context context, CommunityDynamicBean dynamicBean, boolean isFromUserCenter) {
        super(context);
        mDynamicBean = dynamicBean;
        mIsFromUserCenter = isFromUserCenter;

        mOnClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DynamicCommentBean bean = (DynamicCommentBean) v.getTag();
                if (bean == null) {
                    return;
                }
                String uid = bean.getUid();

                if (!TextUtils.isEmpty(uid) && uid.equals(CommonAppConfig.getInstance().getUid())) {
                    chooseVideo(bean.getId());//弹框删除
//                    arrayList.clear();
//                    arrayList.add("删除我的评论");
//                    arrayList.add("取消");
//                    showpop(v, arrayList,bean.getId());
                } else if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClick(bean, 0);//回复
                }
            }
        };
        mLikeDrawable = ContextCompat.getDrawable(context, R.mipmap.icon_up);
        mUnLikeDrawable = ContextCompat.getDrawable(context, R.mipmap.icon_unup);
        mLikeCommentCallback = new HttpCallback() {
            @Override
            public void onSuccess(int code, String msg, String info) {
                if (code == 0 && StringUtil.isNotEmpty(info) && mCurLikeCommentBean != null) {
                    String result = PitUtil.strGetFirst(info);
                    JSONObject obj = JSON.parseObject(result);
                    int like = obj.getIntValue("islike");
                    String likeNum = obj.getString("nums");
                    if (mCurLikeCommentBean != null) {
                        mCurLikeCommentBean.setIsLike(like);
                        mCurLikeCommentBean.setLikeNum(likeNum);
                        notifyItemChanged(mCurLikeCommentPosition, Constants.PAYLOAD);
                    }
                } else {
                    ToastUtil.show(msg);
                }
            }
        };
        mLikeClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Object tag = v.getTag();
                if (tag == null) {
                    return;
                }
                DynamicCommentBean bean = (DynamicCommentBean) tag;
                String uid = bean.getUid();
                if (!TextUtils.isEmpty(uid) && uid.equals(CommonAppConfig.getInstance().getUid())) {
                    ToastUtil.show(R.string.video_comment_cannot_self);
                    return;
                }
                mCurLikeCommentPosition = bean.getPosition();
                mCurLikeCommentBean = bean;
                DynamicHttpUtil.setCommentLike(bean.getId(), mLikeCommentCallback);
            }
        };
        mExpandClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Object tag = v.getTag();
                if (tag != null && mActionListener != null) {
                    mActionListener.onExpandClicked((DynamicCommentBean) tag);
                }
            }
        };
        mCollapsedClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Object tag = v.getTag();
                if (tag == null) {
                    return;
                }
                DynamicCommentBean commentBean = (DynamicCommentBean) tag;
                if (mCurVoiceCommentBean != null) {
                    DynamicCommentBean parentNodeBean = commentBean.getParentNodeBean();
                    if (parentNodeBean != null) {
                        List<DynamicCommentBean> childList = parentNodeBean.getChildList();
                        if (childList != null && childList.size() > 0) {
                            for (DynamicCommentBean child : childList) {
                                if (mCurVoiceCommentBean.getId().equals(child.getId())) {
                                    if (mActionListener != null) {
                                        mActionListener.onVoiceStop(mCurVoiceCommentBean);
                                    }
                                    mCurVoiceCommentBean = null;
                                    break;
                                }
                            }
                        }
                    }
                }
                if (mActionListener != null) {
                    mActionListener.onCollapsedClicked(commentBean);
                }
            }
        };
        mVoiceClickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Object tag = v.getTag();
                if (tag == null) {
                    return;
                }
                DynamicCommentBean commentBean = (DynamicCommentBean) tag;
                if (mCurVoiceCommentBean != null) {
                    if (mCurVoiceCommentBean.getId().equals(commentBean.getId())) {
                        if (commentBean.isVoicePlaying()) {
                            commentBean.setVoicePlaying(false);
                            if (mActionListener != null) {
                                mActionListener.onVoicePause(mCurVoiceCommentBean);
                            }
                        } else {
                            commentBean.setVoicePlaying(true);
                            if (mActionListener != null) {
                                mActionListener.onVoiceResume(mCurVoiceCommentBean);
                            }
                        }
                        //mCurVoiceCommentBean = null;
                    } else {
                        TextView time = (TextView) ((ViewGroup) v).getChildAt(0);
                        mCurVoiceCommentBean.setVoicePlaying(false);
                        notifyItemChanged(mCurVoiceCommentBean.getPosition(), Constants.PAYLOAD);
                        if (mActionListener != null) {
                            mActionListener.onVoiceStop(mCurVoiceCommentBean);
                            mActionListener.onVoicePlay(commentBean, time);
                        }
                        commentBean.setVoicePlaying(true);
                        mCurVoiceCommentBean = commentBean;
                    }
                } else {
                    TextView time = (TextView) ((ViewGroup) v).getChildAt(0);
                    commentBean.setVoicePlaying(true);
                    mCurVoiceCommentBean = commentBean;
                    if (mActionListener != null) {
                        mActionListener.onVoicePlay(commentBean, time);
                    }

                }
                notifyItemChanged(commentBean.getPosition(), Constants.PAYLOAD);

            }
        };

        mVoicePlay = ContextCompat.getDrawable(context, R.mipmap.icon_comment_voice_1);
        mVoicePause = ContextCompat.getDrawable(context, R.mipmap.icon_comment_voice_0);
    }

    private ArrayList<String> arrayList = new ArrayList();
    private PopupWindow mPopWindow;
    private RecyclerView mRecyclerView;
    private NewDynamicPopAdapter mAdapter;


    public void delDynamic(final String delid) {
        KLog.e("log","////="+delid);
        DynamicHttpUtil.delDynamicpl(delid, new HttpCallback() {
            @Override
            public void onSuccess(int code, String msg, String info) {
                if (code == 0) {
                    ((AbsDynamicCommentActivity) mContext).refreshComment();
                }
                ToastUtil.show(msg);
            }

            @Override
            public void onError(Response<JsonBean> response) {
                super.onError(response);
            }
        });
    }

    @Override
    public void refreshData(List<DynamicCommentBean> list) {
        ((AbsDynamicActivity) mContext).stopVoice();
        super.refreshData(list);
    }

    /**
     * 停止语音播放动画
     */
    public void stopVoiceAnim() {
        if (mCurVoiceCommentBean != null) {
            mCurVoiceCommentBean.setVoicePlaying(false);
            notifyItemChanged(mCurVoiceCommentBean.getPosition(), Constants.PAYLOAD);
        }
        mCurVoiceCommentBean = null;
    }

    public void notifyComments(String comments) {
        mDynamicBean.setComments(comments);
        notifyItemChanged(0, Constants.PAYLOAD);
    }

    @Override
    public int getItemCount() {
        int count = 0;
        for (int i = 0, size = mList.size(); i < size; i++) {
            count++;
            List<DynamicCommentBean> childList = mList.get(i).getChildList();
            if (childList != null) {
                count += childList.size();
            }
        }
        return count + 1;
    }

    private DynamicCommentBean getItem(int position) {
        int index = 0;
        for (int i = 0, size = mList.size(); i < size; i++) {
            DynamicCommentBean parentNode = mList.get(i);
            if (index == position) {
                return parentNode;
            }
            index++;
            List<DynamicCommentBean> childList = mList.get(i).getChildList();
            if (childList != null) {
                for (int j = 0, childSize = childList.size(); j < childSize; j++) {
                    if (position == index) {
                        return childList.get(j);
                    }
                    index++;
                }
            }
        }
        return null;
    }


    /**
     * 展开子评论
     *
     * @param childNode   在这个子评论下面插入子评论
     * @param insertCount 插入的子评论的个数
     */
    public void insertReplyList(DynamicCommentBean childNode, int insertCount) {
        //这种方式也能达到  notifyItemRangeInserted 的效果，而且不容易出问题
        if (mRecyclerView != null) {
            mRecyclerView.scrollToPosition(childNode.getPosition());
        }
        notifyItemRangeChanged(childNode.getPosition(), getItemCount(), Constants.PAYLOAD);
    }

    /**
     * 收起子评论
     *
     * @param childNode   在这个子评论下面收起子评论
     * @param removeCount 被收起的子评论的个数
     */
    public void removeReplyList(DynamicCommentBean childNode, int removeCount) {
        //这种方式也能达到  notifyItemRangeRemoved 的效果，而且不容易出问题
        if (mRecyclerView != null) {
            mRecyclerView.scrollToPosition(childNode.getPosition());
        }
        notifyItemRangeChanged(childNode.getPosition(), getItemCount(), Constants.PAYLOAD);
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return HEAD;
        }
        DynamicCommentBean bean = getItem(position - 1);
        if (bean != null) {
            if (bean.isParentNode()) {
                if (bean.isVoice()) {
                    return VOICE_PARENT;
                }
                return TEXT_PARENT;
            } else {
                if (bean.isVoice()) {
                    return VOICE_CHILD;
                }
            }
        }
        return TEXT_CHILD;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == HEAD) {
            return new HeadVh(mInflater.inflate(R.layout.view_dynamic_top, parent, false));
        } else if (viewType == TEXT_PARENT) {
            return new ParentVh(mInflater.inflate(R.layout.item_dynamic_comment_parent, parent, false));
        } else if (viewType == TEXT_CHILD) {
            return new ChildVh(mInflater.inflate(R.layout.item_dynamic_comment_child, parent, false));
        } else if (viewType == VOICE_PARENT) {
            return new VoiceParentVh(mInflater.inflate(R.layout.item_dynamic_comment_parent_voice, parent, false));
        }
        return new VoiceChildVh(mInflater.inflate(R.layout.item_dynamic_comment_child_voice, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder vh, int position, @NonNull List payloads) {
        Object payload = payloads.size() > 0 ? payloads.get(0) : null;
        if (vh instanceof HeadVh) {
            ((HeadVh) vh).setData(payload);
        } else {
            DynamicCommentBean bean = getItem(position - 1);
            if (bean != null) {
                bean.setPosition(position);
                if (vh instanceof ParentVh) {
                    ((ParentVh) vh).setData(bean, payload);
                } else if (vh instanceof ChildVh) {
                    ((ChildVh) vh).setData(bean, payload);
                }
            }
        }

    }

    class HeadVh extends RecyclerView.ViewHolder {
        private View mHeadView;
        private ImageView mAvatar;
        private TextView mName;
        private TextView mTvCity;
        private TextView mTitle;
        private TextView mTime;
        private FrameLayout mContainer;
        ImageView iv_flag;
        private FrameLayout mContainerVideo;
        private DrawableTextView mLike;
        private DrawableTextView mComment;
        private View mBtnSetting;
        private View noDataView;
        private ImageView mIvSex;
        private DrawableTextView dTvAge;
        private LinearLayout ll_city_genderbg;
        private ImageView im_zg;


        private ImageView isAuth;
        private ImageView ivDaRen;
        private ImageView ivGuiZu;
        private ImageView iv_youzhi;

        private HeadVh(View itemView) {
            super(itemView);
            mHeadView = itemView.findViewById(R.id.head_content);
            mAvatar = itemView.findViewById(R.id.avatar);
            mName = itemView.findViewById(R.id.name);
            iv_flag = itemView.findViewById(R.id.iv_flag);
            mTvCity = itemView.findViewById(R.id.city);
            im_zg = itemView.findViewById(R.id.im_zg);
            mTitle = itemView.findViewById(R.id.content);
            mLike = itemView.findViewById(R.id.like);
            mTime = itemView.findViewById(R.id.time);
            mComment = itemView.findViewById(R.id.comment);
            mContainer = itemView.findViewById(R.id.container);
            mContainerVideo = itemView.findViewById(R.id.container_video);
            mBtnSetting = itemView.findViewById(R.id.btn_setting);
            noDataView = itemView.findViewById(R.id.no_data);

            isAuth = itemView.findViewById(R.id.iv_flag);
            ivDaRen = itemView.findViewById(R.id.iv_daren);
            ivGuiZu = itemView.findViewById(R.id.iv_guizu);
             iv_youzhi =itemView.findViewById(R.id.iv_youzhi);

            dTvAge = itemView.findViewById(R.id.tv_age);
            ll_city_genderbg = itemView.findViewById(R.id.ll_city_genderbg);
            mLike.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String uid = mDynamicBean.getUid();
                    if (!TextUtils.isEmpty(uid) && uid.equals(CommonAppConfig.getInstance().getUid())) {
                        ToastUtil.show(R.string.video_comment_cannot_self);
                        return;
                    }
                    DynamicHttpUtil.addLike(mDynamicBean.getId(), new HttpCallback() {
                        @Override
                        public void onSuccess(int code, String msg, String info) {
                            if (code == 0) {
                                String result = PitUtil.strGetFirst(info);
                                JSONObject obj = JSON.parseObject(result);
                                int islike = obj.getIntValue("islike");
                                String likes = obj.getString("nums");
                                mDynamicBean.setIslike(islike + "");
                                mDynamicBean.setLikes(likes);
                                notifyItemChanged(0, Constants.PAYLOAD);
                                EventBus.getDefault().post(new DynamicLikeEvent(mDynamicBean.getId(), islike, likes));
                            } else {
                                ToastUtil.show(msg);
                            }
                        }
                    });
                }
            });

            LinearLayout.LayoutParams sv = (LinearLayout.LayoutParams) mContainerVideo.getLayoutParams();
            sv.width = ScreenDimenUtil.getInstance().getScreenHeight() - DpUtil.px2dip(mContext, 32f);
            mContainerVideo.setLayoutParams(sv);
            mComment.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((AbsDynamicCommentActivity) mContext).openCommentInputWindow(false, mDynamicBean.getId(), mDynamicBean.getUid(), null, "");
                }
            });
            mHeadView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!mIsFromUserCenter) {
                        RouteUtil.forwardUserHome(mDynamicBean.getUid());
                    }

                }
            });
            mBtnSetting.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((AbsDynamicActivity) mContext).setting(mDynamicBean);
                }
            });
            im_zg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    setCommunityAttention(mDynamicBean.getUid());
                }
            });
        }

        /**
         * 关注
         */
        private void setCommunityAttention(String id) {
            CommonHttpUtil.setCommunityAttention(id, new HttpCallback() {
                @Override
                public void onSuccess(int code, String msg, String info) {
                    if (code == 0) {
                        String result = PitUtil.strGetFirst(info);

                        JSONObject obj = JSON.parseObject(result);
                        String isattent = obj.getString("isattent");//0表示未关注，1表示已关注
                        if (isattent.equals("0")) {
                            mDynamicBean.setIsattent(0);
                            im_zg.setImageResource(R.mipmap.pic_gz);
                        } else {
                            mDynamicBean.setIsattent(1);
                            im_zg.setImageResource(R.mipmap.pic_ygz);
                        }

                    }
                }
            });

        }

        //  Glide.with(context).load(item.userinfo.immortal).into(isAuth)
        void setData(Object payload) {

            if (payload == null) {
                CommunityDynamicBean.UserinfoBean userbean = mDynamicBean.getUserinfo();
                if ((mDynamicBean.getUserinfo().getIsauth() + "").equals("1")) {
                    Glide.with(mContext).load(userbean.getImmortal()).into(iv_flag);
                } else {
                }
//                ImgLoader.display(mContext, userbean.getAvatar(), mAvatar);22121
                Glide.with(mContext).load(userbean.getAvatar()).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(mAvatar);
                mName.setText(userbean.getUser_nickname());
                if (TextUtils.isEmpty(mDynamicBean.getTitle())) {
                    mTitle.setVisibility(View.GONE);
                } else {
                    mTitle.setVisibility(View.VISIBLE);
                    mTitle.setText(mDynamicBean.getTitle());
                }
                if (CommonAppConfig.getInstance().getUid().equals(mDynamicBean.getUid())) {
                    im_zg.setVisibility(View.GONE);
                } else {
                    if (mDynamicBean.getIsattent() == 1) {
                        im_zg.setImageResource(R.mipmap.pic_ygz);
                    } else {

                    }
                }
                dTvAge.setText(mDynamicBean.getUserinfo().getAge() + "");

             //   mTime.setText(mDynamicBean.getDatetime());
                mTvCity.setText(mDynamicBean.getCity());
                if (mDynamicBean.getType() == Constants.DYNAMIC_TYPE_IMG) {
                    ((DynamicDetailsActivity) mContext).loadImg(mContainer);
                } else if (mDynamicBean.getType() == Constants.DYNAMIC_TYPE_VIDEO) {
                    ((DynamicDetailsActivity) mContext).loadVideo(mContainerVideo);
                } else if (mDynamicBean.getType() == Constants.DYNAMIC_TYPE_VOICE) {
                    ((DynamicDetailsActivity) mContext).loadVoice(mContainer);
                }


                //男女图标背景
                if (mDynamicBean.getUserinfo() != null) {
                    if (mDynamicBean.getUserinfo().getSex().equals("1")) {
                        dTvAge.setLeftDrawable(mContext.getDrawable(R.mipmap.gender_nan));
                        ll_city_genderbg.setBackgroundResource(R.drawable.shape_item_city_bg);
                    } else {
                        dTvAge.setLeftDrawable(mContext.getDrawable(R.mipmap.gender_nv));
                        ll_city_genderbg.setBackgroundResource(R.drawable.shape_item_city_bg);
                    }
                }



                //是否优质
                if (StringUtil.isNotEmpty(mDynamicBean.getUserinfo().getQuality())) {
                    Glide.with(mContext).load(mDynamicBean.getUserinfo().getQuality()).into(iv_youzhi);
                    iv_youzhi.setVisibility(View.VISIBLE);
                } else {
                    iv_youzhi.setVisibility(View.GONE);
                }
                //是否认证
                if (mDynamicBean.getUserinfo().getIsauth().equals("1")) {
                    Glide.with(mContext).load(mDynamicBean.getUserinfo().getImmortal()).into(isAuth);
                    isAuth.setVisibility(View.VISIBLE);
                } else {
                    isAuth.setVisibility(View.GONE);
                }
                //是否达人
                if (StringUtil.isNotEmpty(mDynamicBean.getUserinfo().getLevel_icon())) {
                    Glide.with(mContext).load(mDynamicBean.getUserinfo().getLevel_icon()).into(ivDaRen);
                    ivDaRen.setVisibility(View.VISIBLE);
                } else {
                    ivDaRen.setVisibility(View.GONE);
                }
                //是否贵族
                if (StringUtil.isNotEmpty(mDynamicBean.getUserinfo().getGzu_icon())) {
                    Glide.with(mContext).load(mDynamicBean.getUserinfo().getGzu_icon()).into(ivGuiZu);
                    ivGuiZu.setVisibility(View.VISIBLE);
                } else {
                    ivGuiZu.setVisibility(View.GONE);
                }
            }

            if (mList.size() == 0) {
                noDataView.setVisibility(View.VISIBLE);
            } else {
                noDataView.setVisibility(View.GONE);
            }
            mLike.setText(mDynamicBean.getLikes());
            mComment.setText(mDynamicBean.getComments());
            if (mDynamicBean.getIslike().equals("1")) {
                mLike.setLeftDrawable(mLikeDrawable);
            } else {
                mLike.setLeftDrawable(mUnLikeDrawable);
            }

        }

    }


    class Vh extends RecyclerView.ViewHolder {
        TextView mName;
        TextView mContent;
        TextView mTime;
        View myitemView;
        public Vh(View itemView) {
            super(itemView);
            mName = itemView.findViewById(R.id.name);
            mContent = itemView.findViewById(R.id.content);
            mTime = itemView.findViewById(R.id.time);
            myitemView = itemView;


        }

        void setData(DynamicCommentBean bean, Object payload) {
            myitemView.setTag(bean);
            myitemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {

                    if (bean == null) {
                        return;
                    }
                    String uid = bean.getUid();

                    if (!TextUtils.isEmpty(uid) && uid.equals(CommonAppConfig.getInstance().getUid())) {
                        chooseVideo(bean.getId());//弹框删除
//                    arrayList.clear();
//                    arrayList.add("删除我的评论");
//                    arrayList.add("取消");
//                    showpop(v, arrayList,bean.getId());
                    } else if (mOnItemClickListener != null) {
                        mOnItemClickListener.onItemClick(bean, 0);//回复
                    }
                }
            });
            KLog.e("log","内容="+bean.getContent()+"//////id="+ bean.getId());
            if (payload == null) {
                UserBean u = bean.getUserBean();
                if (u != null) {
                    mName.setText(u.getUserNiceName());
                }
                if (mContent != null && bean.getContent() != null) {
                    //  mContent.setText(bean.getContent());
                    //    mContent.setText(ImTextRender.renderChatMessage(bean.getContent()));
                    mContent.setText(ImTextRender.renderVideoComment(bean.getContent()+"     ",bean.getDatetime()));
                //    mTime.setText(bean.getDatetime());
                }
            }
        }
    }

    class ParentVh extends Vh {

        //        ImageView mBtnLike;
        DrawableTextView mBtnLike;
        //TextView mLikeNum;
        ImageView mAvatar;


        private ParentVh(View itemView) {
            super(itemView);
            //mBtnLike = (ImageView) itemView.findViewById(R.id.btn_like);
            mBtnLike = itemView.findViewById(R.id.btn_like);
            //mLikeNum = (TextView) itemView.findViewById(R.id.like_num);
            mBtnLike.setOnClickListener(mLikeClickListener);
            mAvatar = itemView.findViewById(R.id.avatar);
        }

        void setData(final DynamicCommentBean bean, Object payload) {
            super.setData(bean, payload);
            if (payload == null) {
                UserBean u = bean.getUserBean();
                if (u != null) {
//                    ImgLoader.displayAvatar(mContext, u.getAvatar(), mAvatar);1211
                    Glide.with(mContext).load(u.getAvatar()).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(mAvatar);
                }
            }
            mAvatar.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    RouteUtil.forwardUserHome(bean.getUid());
                }
            });
            mBtnLike.setTag(bean);
            boolean like = bean.getIsLike() == 1;
            mBtnLike.setTopDrawable(like ? mLikeDrawable : mUnLikeDrawable);
            mBtnLike.setText(bean.getLikeNum());
            //mBtnLike.setTextColor(like ? mLikeColor : mUnLikeColor);
        }
    }

    class ChildVh extends Vh {
        View mBtnGroup;
        View mBtnExpand;//展开按钮
        View mBtnbCollapsed;//收起按钮
        TextView mTvChildAllCommentNum;

        private ChildVh(View itemView) {
            super(itemView);
            mBtnGroup = itemView.findViewById(R.id.btn_group);
            mBtnExpand = itemView.findViewById(R.id.btn_expand);
            mBtnbCollapsed = itemView.findViewById(R.id.btn_collapsed);
            mTvChildAllCommentNum = itemView.findViewById(R.id.comment_num);
            mBtnExpand.setOnClickListener(mExpandClickListener);
            mBtnbCollapsed.setOnClickListener(mCollapsedClickListener);
        }

        void setData(DynamicCommentBean bean, Object payload) {
            super.setData(bean, payload);
            mBtnExpand.setTag(bean);
            mBtnbCollapsed.setTag(bean);
            DynamicCommentBean parentNodeBean = bean.getParentNodeBean();
            if (bean.needShowExpand(parentNodeBean)) {
                if (mBtnGroup.getVisibility() != View.VISIBLE) {
                    mBtnGroup.setVisibility(View.VISIBLE);
                }
                if (mBtnbCollapsed.getVisibility() == View.VISIBLE) {
                    mBtnbCollapsed.setVisibility(View.INVISIBLE);
                }
                if (mBtnExpand.getVisibility() != View.VISIBLE) {
                    mBtnExpand.setVisibility(View.VISIBLE);
                }
                mTvChildAllCommentNum.setText(String.format(WordUtil.getString(R.string.all_comments_replays), parentNodeBean.getReplyNum()-1));
            } else if (bean.needShowCollapsed(parentNodeBean)) {
                if (mBtnGroup.getVisibility() != View.VISIBLE) {
                    mBtnGroup.setVisibility(View.VISIBLE);
                }
                if (mBtnExpand.getVisibility() == View.VISIBLE) {
                    mBtnExpand.setVisibility(View.INVISIBLE);
                }
                if (mBtnbCollapsed.getVisibility() != View.VISIBLE) {
                    mBtnbCollapsed.setVisibility(View.VISIBLE);
                }
            } else {
                if (mBtnGroup.getVisibility() == View.VISIBLE) {
                    mBtnGroup.setVisibility(View.GONE);
                }
            }
        }
    }

    private class VoiceParentVh extends ParentVh {
        TextView mTime;
        AudioMp3View mBubble;


        private VoiceParentVh(View itemView) {
            super(itemView);
            mTime = itemView.findViewById(R.id.time);
            mBubble = itemView.findViewById(R.id.amv_view);
            mBubble.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((DynamicDetailsActivity) mContext).playmp3(mBubble);
                }
            });
        }

        void setData(DynamicCommentBean bean, Object payload) {
            super.setData(bean, payload);
            mBubble.setTag(bean.getVoiceLink());
            mBubble.setMp3(bean.getVoiceLink());
            mBubble.setTextLeng(bean.getVoiceDuration());
            mTime.setText(bean.getDatetime());

        }
    }

    //回复语音的语音
    private class VoiceChildVh extends ChildVh {

        TextView mTime;
        AudioMp3View mBubble;

        private VoiceChildVh(View itemView) {
            super(itemView);
            mTime = itemView.findViewById(R.id.time);
            mBubble = itemView.findViewById(R.id.amv_view);

            mBubble.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    ((DynamicDetailsActivity) mContext).playmp3(mBubble);
                }
            });
        }

        void setData(DynamicCommentBean bean, Object payload) {
            super.setData(bean, payload);
            mBubble.setTag(bean.getVoiceLink());
            mBubble.setMp3(bean.getVoiceLink());
            mBubble.setTextLeng(bean.getVoiceDuration());
            mTime.setText(bean.getDatetime());
        }
    }


    public interface ActionListener {
        void onExpandClicked(DynamicCommentBean commentBean);

        void onCollapsedClicked(DynamicCommentBean commentBean);

        void onVoicePlay(DynamicCommentBean commentBean, TextView time);

        void onVoicePause(DynamicCommentBean commentBean);

        void onVoiceResume(DynamicCommentBean commentBean);

        void onVoiceStop(DynamicCommentBean commentBean);
    }

    public void setActionListener(ActionListener actionListener) {
        mActionListener = actionListener;
    }

}
